package com.hyx.api.service.impl.pc_admin;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.hyx.api.domain.Wallet;
import com.hyx.api.mapper.WalletMapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.hyx.api.mapper.WalletChangeInfoMapper;
import com.hyx.api.domain.WalletChangeInfo;
import com.hyx.api.domain.bo.WalletChangeInfoBo;
import com.hyx.api.domain.vo.WalletChangeInfoVo;
import com.hyx.api.service.pc_admin.IWalletChangeInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

/**
 * 钱包收支Service业务层处理
 *
 * @author HK
 * @date 2023-10-26
 */
@RequiredArgsConstructor
@Service
public class WalletChangeInfoServiceImpl implements IWalletChangeInfoService
{
    private final WalletChangeInfoMapper walletChangeInfoMapper;

    private final WalletMapper walletMapper;

    @Override
    @Transactional
    public Boolean recharge(WalletChangeInfoBo bo) {

        if(bo.getMoney().compareTo(new BigDecimal(0)) == 0)
            throw new RuntimeException("金额不能为零");

        Wallet wallet = walletMapper.selectById(bo.getWalletId());
        if(ObjectUtils.isEmpty(wallet))
            throw new RuntimeException("没有查询到对应的钱包！");
        //充值
        int num = walletMapper.addMoney(bo.getMoney(), bo.getWalletId());

        if(num > 0) {
            WalletChangeInfo info = tranEntityBeforeSave(bo);
            info.setStatus(2);
            if(bo.getMoney().compareTo(new BigDecimal(0)) < 0) {
                info.setType(2);
            }else {
                info.setType(1);
            }
            info.setCreateBy(SecurityUtils.getUsername());
            info.setCreateTime(new Date());
            walletChangeInfoMapper.insert(info);
        }

        return num > 0;
    }

    /**
     * 查询钱包收支列表
     *
     * @param bo 钱包收支
     * @return 钱包收支
     */
    @Override
    public List<WalletChangeInfoVo> selectWalletChangeInfoList(WalletChangeInfoBo bo)
    {
        LambdaQueryWrapper<WalletChangeInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtils.isNotEmpty(bo.getType()), WalletChangeInfo::getType, bo.getType());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getMoney()), WalletChangeInfo::getMoney, bo.getMoney());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getStatus()), WalletChangeInfo::getStatus, bo.getStatus());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getWalletId()), WalletChangeInfo::getWalletId, bo.getWalletId());
        lqw.orderByDesc(WalletChangeInfo::getCreateTime);
        return walletChangeInfoMapper.selectVoList(lqw);
    }

    @Override
    public BigDecimal nowDayExpend() {
        return walletChangeInfoMapper.nowDayExpend();
    }

    /**
     *   保存前的数据转换
     */
    private WalletChangeInfo tranEntityBeforeSave(WalletChangeInfoBo bo) {
        //TODO 做一些数据转换
        WalletChangeInfo walletChangeInfo = new WalletChangeInfo();
        BeanUtils.copyProperties(bo, walletChangeInfo);
        return walletChangeInfo;
    }
}
